package com.question.controller.user;

import com.question.constant.RedisKeyConstant;
import com.question.pojo.dto.UserFavoriteQuestionDTO;
import com.question.pojo.result.Result;
import com.question.pojo.vo.UserFavoriteQuestionVO;
import com.question.service.FavoriteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users/favorite")
@Slf4j
@Api(tags = "用户收藏相关接口")
public class FavoriteController {

    @Autowired
    private FavoriteService favoriteService;

    /**
     * 添加取消收藏
     * @param questionId
     * @return
     */
    @PostMapping("/{questionId}")
    @ApiOperation("添加取消收藏")
    @Caching(evict = {@CacheEvict(cacheNames = RedisKeyConstant.REDIS_USER_LISTALL_KEY, allEntries = true),
            @CacheEvict(cacheNames = RedisKeyConstant.REDIS_USER_LISTPAGE_KEY, allEntries = true),
            @CacheEvict(cacheNames = RedisKeyConstant.REDIS_USER_LISTFAVORITE_KEY, allEntries = true)
    })
    public Result addOrCancel(@PathVariable Long questionId){
        log.info("添加取消收藏：{}", questionId);
        favoriteService.addOrCancel(questionId);
        return Result.success();
    }

    /**
     * 用户收藏题目查询
     * @param favoriteQuestionDTO
     * @return
     */
    @GetMapping("/get")
    @ApiOperation("用户收藏题目查询")
    @Cacheable(cacheNames = RedisKeyConstant.REDIS_USER_LISTFAVORITE_KEY, key = "#favoriteQuestionDTO.hashCode() + ':' + T(com.question.context.BaseContext).currentId")
    public Result<List<UserFavoriteQuestionVO>> get(UserFavoriteQuestionDTO favoriteQuestionDTO){
        log.info("用户收藏题目查询:{}", favoriteQuestionDTO);
        List<UserFavoriteQuestionVO> userFavoriteQuestionVOList = favoriteService.getMyFavoriteQuestions(favoriteQuestionDTO);
        return Result.success(userFavoriteQuestionVOList);
    }
}
